import { AdManager } from "../ads/AdManager";
import { BUNDLE_TYPE_ENUM } from "../base/EnumIndex";
import { Tools } from "../common/Tools";
import { ResManager } from "./ResManager";

/** 音效管理类 */
export class AudioManager {
    public static bgAudio: number;  //背景音乐id
    private static bgmUrl: string;  //当前背景音乐路径
    private static bgmBundle: BUNDLE_TYPE_ENUM;//当前背景音乐所在bundle

    public static isPlayAudio: boolean = true;                //是否播放音效
    public static isPlayBG: boolean = true;                   //是否播放音乐
    public static isVib: boolean = true;                        //是否震动
    public static isSubtitle: boolean = true;                        //是否开启字幕（黑条与气泡）
    public static isDialogue: boolean = true;                        //是否开启关内人物对话

    public static audioDic: Map<string, number> = new Map();    //存储当前播放的音效

    /** 初始化本地数据 */
    public static initStorage() {
        let isPlayAudio = Tools.getStorage("isPlayAudio");
        let isPlayBG = Tools.getStorage("isPlayBG");
        let isVib = Tools.getStorage("isVib");
        let isSubtitle = Tools.getStorage("isSubtitle");
        let isDialogue = Tools.getStorage("isDialogue");

        this.isPlayAudio = isPlayAudio || isPlayAudio == null ? true : false;
        this.isPlayBG = isPlayBG || isPlayBG == null ? true : false;
        this.isVib = isVib || isVib == null ? true : false;
        this.isSubtitle = isSubtitle || isSubtitle == null ? true : false;
        this.isDialogue = isDialogue || isDialogue == null ? true : false;
    };
    /** 设置  音乐是否开始 并记录本地数据 */
    public static setMusicStorage(isMusic: boolean) {
        this.isPlayBG = isMusic;
        Tools.setStorage("isPlayBG", isMusic ? 1 : 0);
    };
    /** 设置  音乐是否开始 并记录本地数据 */
    public static setAudioStorage(isAudio: boolean) {
        this.isPlayAudio = isAudio;
        Tools.setStorage("isPlayAudio", isAudio ? 1 : 0);
    };
    /** 设置  震动是否开始 并记录本地数据 */
    public static setVibStorage(isVib: boolean) {
        this.isVib = isVib;
        Tools.setStorage("isVib", isVib ? 1 : 0);
    };
    /** 设置  是否开启字幕（黑条与气泡） 并记录本地数据 */
    public static setSubtitleStorage(isSubtitle: boolean) {
        this.isSubtitle = isSubtitle;
        Tools.setStorage("isSubtitle", isSubtitle ? 1 : 0);
    };
    /** 设置  是否开启关内人物对话 并记录本地数据 */
    public static setDialogueStorage(isDialogue: boolean) {
        this.isDialogue = isDialogue;
        Tools.setStorage("isDialogue", isDialogue ? 1 : 0);
    };


    /**
     * 播放背景音乐
     * @param audioUrl 
     * @param value 音乐音量
     * @returns 
     */
    public static async playBG(audioUrl: string, value = 0.5, bundleType = BUNDLE_TYPE_ENUM.MAIN) {
        if (!this.isPlayBG) return;
        if (audioUrl === this.bgmUrl && bundleType === this.bgmBundle) {
            cc.log("同一背景音乐")
            // 同一背景音乐
            return;
        }
        this.stopBG();
        this.bgmUrl = audioUrl;
        this.bgmBundle = bundleType;
        let audio = await ResManager.ins.loadRes('audio/' + audioUrl, cc.AudioClip, bundleType);
        this.bgAudio = cc.audioEngine.playMusic(audio, true);
        cc.audioEngine.setMusicVolume(value);
    };
    /** 停止播放背景音乐 */
    public static stopBG() {
        this.bgmUrl = null;
        this.bgmBundle = null;
        this.stopAudio(this.bgAudio);
    };


    public static playClick() {
        this.playAudio("xuanzhong", 1);
    }
    /**
     * 播放点击音效
     * @param audioUrl 音效文件的路径
     * @param value 音量大小
     * @param isLoop 是否循环播放
     */
    public static async playClickAudio(audioUrl: string, value: number, bundleType = BUNDLE_TYPE_ENUM.MAIN) {
        if (!this.isPlayBG) return;

        let audio = await ResManager.ins.loadRes('audio/' + audioUrl, cc.AudioClip, bundleType);
        // 关闭配音 关卡bundle内音量设为0
        if (!this.isDialogue && bundleType === BUNDLE_TYPE_ENUM.LEVEL) {
            value = 0;
        }
        return this.playAudioEngine(audio, value);
    };
    /**
     * 播放游戏音效
     * @param audioUrl 音效文件的路径
     * @param value 音量大小
     * @param isLoop 是否循环播放
     */
    public static async playAudio(audioUrl: string, value: number, bundleType = BUNDLE_TYPE_ENUM.MAIN) {
        if (!this.isPlayAudio) return;

        let audio = await ResManager.ins.loadRes('audio/' + audioUrl, cc.AudioClip, bundleType);
        // 关闭配音 关卡bundle内音量设为0
        if (!this.isDialogue && bundleType === BUNDLE_TYPE_ENUM.LEVEL) {
            value = 0;
        }
        return this.playAudioEngine(audio, value);
    };
    /** 播放一个音效 */
    private static playAudioEngine(audio: cc.AudioClip, value: number) {
        let audioID = cc.audioEngine.playEffect(audio, false);
        this.audioDic.set(audio.name, audioID);
        cc.audioEngine.setVolume(audioID, value);
        cc.audioEngine.setFinishCallback(audioID, () => {
            this.audioDic.delete(audio.name);
        });
        return audioID;
    }
    public static setAudioLoop(audioID: number, loop: boolean) {
        cc.audioEngine.setLoop(audioID, loop);
    }
    public static setCallback(audioID: number, callback: Function) {
        cc.audioEngine.setFinishCallback(audioID, () => {
            callback && callback();
        });
    }
    /** 停止播放所有音效 包含音乐 */
    public static stopAllAudio() {
        this.stopBG();
        this.stopAllAudioDir();
        // cc.audioEngine.stopAll();
        this.audioDic.clear();
    };
    /** 停止播放所有存到字典音效 */
    public static stopAllAudioDir(exclue?: string) {
        this.audioDic.forEach((element, key) => {
            if (exclue !== key) {
                this.stopAudio(element);
                this.audioDic.delete(key);
            }
        });
        // cc.audioEngine.stopAll();
        // this.audioDic.clear();
    };
    /** 停止播放某个音效 */
    public static stopAudio(audioId: number) {
        cc.audioEngine.stop(audioId);
    };
    /** 停止通过音效名字  停止一个音效 */
    public static stopAudioForName(audioE?: string) {
        if (audioE != null) {
            this.stopAudio(this.audioDic.get(audioE));
            this.audioDic.delete(audioE);
        }
    };

    /** 暂停背景音乐 */
    public static pauseMusic() {
        cc.audioEngine.pauseMusic();
    };
    /** 恢复背景音乐 */
    public static resumeMusic() {
        cc.audioEngine.resumeMusic();
    };

    /** 设置背景音乐的音量 */
    public static setBgValue(value: number) {
        if (this.bgAudio != null) {
            cc.audioEngine.setMusicVolume(value);
            // cc.audioEngine.setVolume(this.bgAudio, value);
        }
    };

    /** 手机震动  num震动时间  毫秒 */
    public static vibrateIphone(num = 200) {
        if (!this.isVib) { return; }
        AdManager.vibrate(num);
    }
}